﻿import { BAR } from './chartTypes'
import { SUM } from './aggrTypes'

function titleOption(text, subtext) { return { x: 'center', text, subtext } }

const pieOptions = (title, subtitle, names, values) => {
    return {
        ...fixedOptions(),
        title: titleOption(title, subtitle),
        series: [serie(subtitle, names, values)]
    }
    function serie(subtitle, names, values) {
        var data = names.map((name, idx) => ({ name, value: values[idx] }));
        return {
            name: subtitle,
            type: 'pie',
            radius: ['30%', '60%'],
            center: ['45%', '50%'],
            data
        }
    }
    function fixedOptions() {
        return {
            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c} ({d}%)"
            },
            legend: {
                type: 'scroll',
                orient: 'vertical',
                left: 'right'
            }
        }
    }
}

const barOptions = (title, subtitle, names, values) => {
    return {
        ...fixedOptions(),
        title: titleOption(title, subtitle),
        xAxis: xAxis(names),
        series: serie(values)
    }
    function xAxis(data) {
        return {
            type: 'category',
            axisTick: { alignWithLabel: true },
            data    // should be string array
        }
    }
    function serie(data) {
        return {
            type: 'bar',
            label: {
                normal: {
                    show: true,
                    position: 'top'
                }
            },
            data    // require [ number,... ]
        }
    }
    function fixedOptions() {
        return {
            yAxis: [{ type: 'value' }],
            tooltip: {
                trigger: 'axis',
                formatter: "{b} : {c}",
                axisPointer: { type: 'shadow' }
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            }
        }
    }
}

const top = {
    topNames(rows, count = 10) {
        if (!rows) return null;

        let tops = rows.slice(0, count).map(d => d[0]);
        if (rows.length > count) tops.push('其余');
        return tops;
    },
    topValues(rows, ci, restType, count = 10) {
        if (!rows) return null;

        let tops = rows.slice(0, count).map(d => d[ci]);
        if (rows.length > count) {
            var sum = rows.slice(count).reduce((sum, row) => sum + row[ci], 0);
            if (restType === SUM)
                tops.push(sum);
            else
                tops.push(sum / (rows.length - count));
        }
        return tops;
    }
}

export default function buildOption(props) {
    var { chartName, data: { title, rows, headers, currentCol } } = props,
        names = top.topNames(rows),
        values = top.topValues(rows, currentCol),
        subtitle = headers[currentCol].text;
    return (chartName === BAR ? barOptions : pieOptions)(title, subtitle, names, values);
}